package com.example.tp1;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Vector;

import com.example.tp1.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings.System;
import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {
	private static final int INVALID_POINTER_ID = 0;
	private TextView coucou = null;
	private EditText edittext;
	private int score = 0;
	private int mActivePointerId = INVALID_POINTER_ID;
	private float mLastTouchX;
	private float mLastTouchY;
	private BoardField mBoardField;
	private Boolean isInit = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_main);
		coucou = new TextView(this);
		coucou.setText("hello word kais");
		setContentView(coucou);

		setContentView(R.layout.activity_game_grid);

	}

	protected void initPosition() {
		TableLayout table = (TableLayout) findViewById(R.id.chessboard);

		float left = table.getLeft();
		float right = table.getRight();
		int[] locationOnScreen = new int[2];
		table.getLocationOnScreen(locationOnScreen);
		float top = locationOnScreen[1];
		mBoardField = new BoardField(top, left, right);
		// Log.i("info", String.valueOf(top) + ", " + String.valueOf(left) +
		// ", "
		// + String.valueOf(right) + table);
		isInit = true;
	}

	public void setMode(View view) {
		Intent intent = new Intent(this, SetModeActivity.class);
		startActivity(intent);

	}

	public void setUsername(View view) {
		Intent intent = new Intent(this, SetUsernameActivity.class);
		startActivity(intent);
	}

	public void leaveApp(View view) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
	 */
	@SuppressLint("NewApi")
	public boolean onTouchEvent(MotionEvent ev) {
		// Let the ScaleGestureDetector inspect all events.
		// mScaleDetector.onTouchEvent(ev);

		final int action = MotionEventCompat.getActionMasked(ev);
		if (!isInit)
			initPosition();
		edittext = (EditText) findViewById(R.id.editText);

		String strColor = "";
		// com.example.tp1.SquareImageView square =
		// (com.example.tp1.SquareImageView) findViewById(R.id.a8);
		int color = Color.TRANSPARENT;
		// Drawable background = square.getBackground();
		// if (background instanceof ColorDrawable)
		// {
		// color = ((ColorDrawable) background).getColor();
		// strColor = String.format("#%06X", 0xFFFFFF & color);
		// }

		switch (action) {
		case MotionEvent.ACTION_DOWN: {

			final int pointerIndex = MotionEventCompat.getActionIndex(ev);
			final float x = MotionEventCompat.getX(ev, pointerIndex);
			final float y = MotionEventCompat.getY(ev, pointerIndex);
			mLastTouchX = x;
			mLastTouchY = y;
			mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

			// if (x > (3 * size + left) && x < (4 * size + left)
			// && y > (0 * size + top)
			// && y < (1 * size + top)) {
			// score += 10;
			// }
			// edittext.setText(String.valueOf(score));
			break;
		}
		case MotionEvent.ACTION_UP: {
			// Find the index of the active pointer and fetch its position
			final int pointerIndex = MotionEventCompat.findPointerIndex(ev,
					mActivePointerId);

			final float x = MotionEventCompat.getX(ev, pointerIndex);
			final float y = MotionEventCompat.getY(ev, pointerIndex);

			// Calculate the distance moved
			final float dx = x - mLastTouchX;
			final float dy = y - mLastTouchY;

			verifyMove(mLastTouchX, mLastTouchY, x, y);

			// if (mLastTouchX > (3 * size + left) && mLastTouchX < (4 * size +
			// left)
			// && mLastTouchY > (0 * size + top) && mLastTouchY < (1 * size +
			// top)
			// && dy>0 && dy>dx)
			// {
			// score += 10;
			// }

			// edittext.setText(String.valueOf(score));
			// edittext.setText(String.valueOf(score) + ", " +
			// String.valueOf(strColor));
			// Remember this touch position for the next move event
			// mLastTouchX = 0;
			// mLastTouchY = 0;

			break;
		}

		}
		return true;
	}

	private void verifyMove(float lastX, float lastY, float x, float y) {
		// com.example.tp1.SquareImageView[] listToClear;
		// int numberToClear = 0;
		ArrayDeque<String> listToClear = new ArrayDeque<String>();
		ArrayDeque<String> listToClearTemp1 = new ArrayDeque<String>();
		ArrayDeque<String> listToClearTemp2 = new ArrayDeque<String>();
		// verify #1
		String[] listSquare = mBoardField.setSquareLine(lastX, lastY, x, y);
		com.example.tp1.SquareImageView[] listSquareObj = getSquareObject(listSquare);
		String[] square = new String[1];
		square[0] = mBoardField.getSquareLoc();
		com.example.tp1.SquareImageView[] squareObj = getSquareObject(square);
		int color = Color.TRANSPARENT;
		Drawable background = squareObj[0].getBackground();
		if (background instanceof ColorDrawable) {
			color = ((ColorDrawable) background).getColor();
		}
		int nbSameColor = 0;

		for (int i = 0; i < listSquareObj.length; i++) {

			if (listSquareObj.length > 4
					&& ((ColorDrawable) listSquareObj[i].getBackground())
							.getColor() != color && i == 2) {
				break;
			}

			else if (listSquareObj.length > 3
					&& ((ColorDrawable) listSquareObj[i].getBackground())
							.getColor() != color && i == 1) {
				break;
			}

			if (((ColorDrawable) listSquareObj[i].getBackground()).getColor() == color) {
				nbSameColor++;
				listToClearTemp1.add(listSquare[i]);
			}
		}
		if (nbSameColor >= 2) {
			score += nbSameColor * 50;
			edittext.setText(String.valueOf(score));
			listToClearTemp1.add(square[0]);
		} else {
			listToClearTemp1.clear();
		}

		// verify #2
		String[] listSquare2 = mBoardField.setSquareLine(x, y, lastX, lastY);
		com.example.tp1.SquareImageView[] listSquareObj2 = getSquareObject(listSquare2);
		String[] square2 = new String[1];
		square2[0] = mBoardField.getSquareLoc();
		com.example.tp1.SquareImageView[] squareObj2 = getSquareObject(square2);
		int color2 = Color.TRANSPARENT;
		Drawable background2 = squareObj2[0].getBackground();
		if (background2 instanceof ColorDrawable) {
			color2 = ((ColorDrawable) background2).getColor();
		}
		int nbSameColor2 = 0;

		for (int i = 0; i < listSquareObj2.length; i++) {

			if (listSquareObj2.length > 4
					&& ((ColorDrawable) listSquareObj2[i].getBackground())
							.getColor() != color2 && i == 2) {
				break;
			}

			else if (listSquareObj2.length > 3
					&& ((ColorDrawable) listSquareObj2[i].getBackground())
							.getColor() != color2 && i == 1) {
				break;
			}

			if (((ColorDrawable) listSquareObj2[i].getBackground()).getColor() == color2) {
				nbSameColor2++;
				listToClearTemp2.add(listSquare[i]);
			}
		}
		if (nbSameColor2 >= 2) {
			score += nbSameColor2 * 50;
			edittext.setText(String.valueOf(score));
			listToClearTemp2.add(square2[0]);
		} else {
			listToClearTemp2.clear();
		}
		for (String squareName : listToClearTemp1) {
			listToClear.add(squareName);
		}
		for (String squareName : listToClearTemp2) {
			listToClear.add(squareName);
		}
		parseRow(listToClear);
	}

	private void parseRow(ArrayDeque<String> listToClear) {
		char letter;
		boolean inserted = false;
		Vector<String> a = new Vector<String>();
		Vector<String> b = new Vector<String>();
		Vector<String> c = new Vector<String>();
		Vector<String> d = new Vector<String>();
		Vector<String> e = new Vector<String>();
		Vector<String> f = new Vector<String>();
		Vector<String> g = new Vector<String>();
		Vector<String> h = new Vector<String>();
		for (String squareName : listToClear) {
			letter = squareName.charAt(0);
			if (letter == 'a') {
				if (a.size() > 0)
				{
					for (int i = 0; i < a.size(); i++)
					{
						if (a.elementAt(i).compareTo(squareName) < 0)
						{
							a.insertElementAt(squareName, i);
							inserted = true;
						}
					}
					if (!inserted)
						a.add(squareName);
				}
				else
				{
					a.add(squareName);
				}
			} else if (letter == 'b') {
				if (b.size() > 0)
				{
					for (int i = 0; i < b.size(); i++)
					{
						if (b.elementAt(i).compareTo(squareName) < 0)
						{
							b.insertElementAt(squareName, i);
							inserted = true;
						}
					}
					if (!inserted)
						b.add(squareName);
				}
				else
				{
					b.add(squareName);
				}
			} else if (letter == 'c') {
				if (c.size() > 0)
				{
					for (int i = 0; i < c.size(); i++)
					{
						if (c.elementAt(i).compareTo(squareName) < 0)
						{
							c.insertElementAt(squareName, i);
							inserted = true;
						}
					}
					if (!inserted)
						c.add(squareName);
				}
				else
				{
					c.add(squareName);
				}
			} else if (letter == 'd') {
				if (d.size() > 0)
				{
					for (int i = 0; i < d.size(); i++)
					{
						if (d.elementAt(i).compareTo(squareName) < 0)
						{
							d.insertElementAt(squareName, i);
							inserted = true;
						}
					}
					if (!inserted)
						d.add(squareName);
				}
				else
				{
					d.add(squareName);
				}
			} else if (letter == 'e') {
				if (e.size() > 0)
				{
					for (int i = 0; i < e.size(); i++)
					{
						if (e.elementAt(i).compareTo(squareName) < 0)
						{
							e.insertElementAt(squareName, i);
							inserted = true;
						}
					}
					if (!inserted)
						e.add(squareName);
				}
				else
				{
					e.add(squareName);
				}
			} else if (letter == 'f') {
				if (f.size() > 0)
				{
					for (int i = 0; i < f.size(); i++)
					{
						if (f.elementAt(i).compareTo(squareName) < 0)
						{
							f.insertElementAt(squareName, i);
							inserted = true;
						}
					}
					if (!inserted)
						f.add(squareName);
				}
				else
				{
					f.add(squareName);
				}
			} else if (letter == 'g') {
				if (g.size() > 0)
				{
					for (int i = 0; i < g.size(); i++)
					{
						if (g.elementAt(i).compareTo(squareName) < 0)
						{
							g.insertElementAt(squareName, i);
							inserted = true;
						}
					}
					if (!inserted)
						g.add(squareName);
				}
				else
				{
					g.add(squareName);
				}
			} else if (letter == 'h') {
				if (h.size() > 0)
				{
					for (int i = 0; i < h.size(); i++)
					{
						if (h.elementAt(i).compareTo(squareName) < 0)
						{
							h.insertElementAt(squareName, i);
							inserted = true;
						}
					}
					if (!inserted)
						h.add(squareName);
				}
				else
				{
					h.add(squareName);
				}
			}
		}
		if (a.size() > 0){
		
		}
		if (d.size() > 0){
			
		}
		if (c.size() > 0){
			
		}
		if (d.size() > 0){
			
		}
		if (e.size() > 0){
			
		}
		if (f.size() > 0){
			
		}
		if (g.size() > 0){
			
		}
		if (h.size() > 0){
			
		}
	}
	
	private void newSquares(Vector<String> squares){
		
	}

	private com.example.tp1.SquareImageView[] getSquareObject(
			String listSquare[]) {
		com.example.tp1.SquareImageView[] squareObjArray = new com.example.tp1.SquareImageView[listSquare.length];
		for (int i = 0; i < listSquare.length; i++) {
			if (listSquare[i] == "a0")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.a0);
			else if (listSquare[i] == "a1")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.a1);
			else if (listSquare[i] == "a2")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.a2);
			else if (listSquare[i] == "a3")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.a3);
			else if (listSquare[i] == "a4")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.a4);
			else if (listSquare[i] == "a5")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.a5);
			else if (listSquare[i] == "a6")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.a6);
			else if (listSquare[i] == "a7")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.a7);

			else if (listSquare[i] == "b0")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.b0);
			else if (listSquare[i] == "b1")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.b1);
			else if (listSquare[i] == "b2")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.b2);
			else if (listSquare[i] == "b3")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.b3);
			else if (listSquare[i] == "b4")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.b4);
			else if (listSquare[i] == "b5")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.b5);
			else if (listSquare[i] == "b6")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.b6);
			else if (listSquare[i] == "b7")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.b7);

			else if (listSquare[i] == "c0")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.c0);
			else if (listSquare[i] == "c1")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.c1);
			else if (listSquare[i] == "c2")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.c2);
			else if (listSquare[i] == "c3")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.c3);
			else if (listSquare[i] == "c4")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.c4);
			else if (listSquare[i] == "c5")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.c5);
			else if (listSquare[i] == "c6")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.c6);
			else if (listSquare[i] == "c7")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.c7);

			else if (listSquare[i] == "d0")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.d0);
			else if (listSquare[i] == "d1")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.d1);
			else if (listSquare[i] == "d2")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.d2);
			else if (listSquare[i] == "d3")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.d3);
			else if (listSquare[i] == "d4")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.d4);
			else if (listSquare[i] == "d5")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.d5);
			else if (listSquare[i] == "d6")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.d6);
			else if (listSquare[i] == "d7")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.d7);

			else if (listSquare[i] == "e0")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.e0);
			else if (listSquare[i] == "e1")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.e1);
			else if (listSquare[i] == "e2")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.e2);
			else if (listSquare[i] == "e3")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.e3);
			else if (listSquare[i] == "e4")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.e4);
			else if (listSquare[i] == "e5")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.e5);
			else if (listSquare[i] == "e6")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.e6);
			else if (listSquare[i] == "e7")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.e7);

			else if (listSquare[i] == "f0")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.f0);
			else if (listSquare[i] == "f1")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.f1);
			else if (listSquare[i] == "f2")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.f2);
			else if (listSquare[i] == "f3")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.f3);
			else if (listSquare[i] == "f4")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.f4);
			else if (listSquare[i] == "f5")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.f5);
			else if (listSquare[i] == "f6")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.f6);
			else if (listSquare[i] == "f7")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.f7);

			else if (listSquare[i] == "g0")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.g0);
			else if (listSquare[i] == "g1")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.g1);
			else if (listSquare[i] == "g2")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.g2);
			else if (listSquare[i] == "g3")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.g3);
			else if (listSquare[i] == "g4")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.g4);
			else if (listSquare[i] == "g5")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.g5);
			else if (listSquare[i] == "g6")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.g6);
			else if (listSquare[i] == "g7")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.g7);

			else if (listSquare[i] == "h0")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.h0);
			else if (listSquare[i] == "h1")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.h1);
			else if (listSquare[i] == "h2")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.h2);
			else if (listSquare[i] == "h3")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.h3);
			else if (listSquare[i] == "h4")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.h4);
			else if (listSquare[i] == "h5")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.h5);
			else if (listSquare[i] == "h6")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.h6);
			else if (listSquare[i] == "h7")
				squareObjArray[i] = (com.example.tp1.SquareImageView) findViewById(R.id.h7);
		}
		return squareObjArray;
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.main, menu);
	// return true;
	// }

}
